﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Vms.DB;
using Vms.Business;
using System.Text;
using System.IO;

public partial class TruyenCuoi_view : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Util m_util = new Util();
        DB m_db = new DB();
        WriteFile wf = new WriteFile();
        int Service_ID = 0;
        string str = "";
        Util.TCharging m_tCharging = new Util.TCharging();
        try
        {
            int e_variable = 0;
            string d_variable = "";
            int id_truyen = 0;

            if (Request.Params["id_truyen"] != null)
            {
                id_truyen = Int32.Parse(Request.Params["id_truyen"]);
            }
            if (Request.Params["service_id"] != null)
            {
                Service_ID = Int32.Parse(Request.Params["service_id"]);
            }
            if (Request.Params["e"] != null)
            {
                e_variable = Int32.Parse(Request.Params["e"]);
            }
            if (Request.Params["desc"] != null)
            {
                d_variable = Request.Params["desc"];
            }
            str = "<?xml version='1.0' encoding='ISO-8859-1'?>" +
                     "<!DOCTYPE pages SYSTEM 'cellflash.dtd'>" +
                     "<pages descr='Samis'>";
            Util.TSession m_tSession = new Util.TSession();
            m_tSession.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
            m_tSession.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
            m_tSession.SessionID = "";
            m_tSession.IP = "";
            m_tSession.ServiceList = "3";
            m_tSession.KeyPress = "3";
            m_db.UpdateReQuestTbl(m_tSession);
            if (id_truyen == 0)
            {
                if (Service_ID == 31)
                {
                    m_tSession.ServiceList = "31";
                    m_tSession.KeyPress = "1";
                    m_db.UpdateReQuestTbl(m_tSession);
                    DataSet ds = new DataSet();
                    ds = m_db.GetListTruyenHot(m_tSession.UserMSISDN);
                    DataTable dtb = new DataTable();
                    dtb = ds.Tables[0].Clone();
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dtb.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                    }

                    str += "<page>" +
                    "Truyen Hot<br/>";
                    foreach (DataRow dr in dtb.Rows)
                    {
                        str += "<a href='/850/sys/bill?a=" + m_db.GetCostByService(Service_ID).ToString() + "&b=0&c=TruyenCuoi&t=0&re=0&i=" + String.Format("{0:000000}", Int32.Parse(Service_ID.ToString().Substring(1, 1))) + ":" + String.Format("{0:0000000000}", Int32.Parse(Service_ID.ToString())) + ":TCuoi&d=TruyenCuoi_view.aspx?id_truyen=" + dr["ID"].ToString() + "'>" + dr["TenTruyen"].ToString().Trim() + "</a><br/>";
                    }

                }
                else if (Service_ID == 32)
                {
                    m_tSession.ServiceList = "32";
                    m_tSession.KeyPress = "2";
                    m_db.UpdateReQuestTbl(m_tSession);
                    DataSet ds = new DataSet();
                    ds = m_db.GetListTruyenVui(m_tSession.UserMSISDN);
                    DataTable dtb = new DataTable();
                    dtb = ds.Tables[0].Clone();
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dtb.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                    }

                    str += "<page>" +
                    "Truyen Vui<br/>";
                    foreach (DataRow dr in dtb.Rows)
                    {
                        str += "<a href='/850/sys/bill?a=" + m_db.GetCostByService(Service_ID).ToString() + "&b=0&c=TruyenCuoi&t=0&re=0&i=" + String.Format("{0:000000}", Int32.Parse(Service_ID.ToString().Substring(1, 1))) + ":" + String.Format("{0:0000000000}", Int32.Parse(Service_ID.ToString())) + ":TCuoi&d=TruyenCuoi_view.aspx?id_truyen=" + dr["ID"].ToString() + "'>" + dr["TenTruyen"].ToString().Trim() + "</a><br/>";
                    }
                }
            }
            else
            {
                m_tSession.ServiceList = "31";
                m_tSession.KeyPress = "1";
                m_db.UpdateReQuestTbl(m_tSession);
                m_tCharging.ErrorCode = e_variable;
                m_tCharging.ErrorDescription = d_variable;
                m_tCharging.Service_ID = 31;
                m_tCharging.SessionID = Session.SessionID;
                m_tCharging.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
                m_tCharging.SessionID = Session.SessionID;
                m_tCharging.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
                m_tCharging.StartTime = DateTime.Now;
                if (e_variable == 0)
                {
                    m_tCharging.Cost = m_db.GetCostByService(m_tCharging.Service_ID);
                    m_tCharging.CDRIndicator = "DATA";
                    str += "<page> <br/>";
                    if (m_db.GetTruyenByID(id_truyen).Length > 0)
                    {
                        str += m_db.GetTruyenByID(id_truyen);
                        m_db.InsertCustumerStory(m_tCharging.UserMSISDN, id_truyen);
                    }
                    else
                    {
                        str += "Hien tai chua co noi dung nay, mong quy khach quay lai sau.";
                    }
                }
                else
                {
                    m_tCharging.Cost = 0;
                    m_util.write_log(Request.Url.OriginalString, 1);
                    m_util.WritelogHeader(Request.Headers);
                    m_tCharging.CDRIndicator = "WRONG";
                    str += "Tai khoan khong du de su dung dich vu. Xin vui long nap tien!";
                }
                m_db.InsertChargingTbl(m_tCharging);

            }
            str += "</page>" +
             "</pages>";
            Response.ClearHeaders();
            Response.AddHeader("content-type", "text/xml");
            Response.Write(str);
            wf.WriteStreamFile(str);
            Response.End();
        }
        catch (Exception ex)
        {
            wf.WriteError(ex.ToString());
        }
    }
}